//
//  CoreErrorsDisplayView.m
//  mobile
//
//  Created by abc on 2018/1/31.
//  Copyright © 2018年 azazie. All rights reserved.
//

#import "CoreErrorsDisplayView.h"

#import "TXFire.h"
#import <QMUIKit/QMUIKit.h>
#import <Masonry/Masonry.h>

@interface CoreErrorsDisplayView ()
{
    CGFloat _labelGap;
    CGFloat _sideSpace;
    CGFloat _topSpace;
    CGFloat _bottomSpace;
    CGFloat _paddingHeight;
    UIFont *_textFont;
    UIColor *_textColor;
}

@property (nonatomic, strong) NSArray *errors;

@end

@implementation CoreErrorsDisplayView

+ (instancetype)errorViewWithGap:(CGFloat)labelGap
                       sideSpace:(CGFloat)sideSpace
                        topSpace:(CGFloat)topSpace
                     bottomSpace:(CGFloat)bottomSpace
                   paddingHeight:(CGFloat)paddingHeight
                        textFont:(UIFont *)textFont
                       textColor:(UIColor *)textColor {
    CoreErrorsDisplayView *errorView = [[CoreErrorsDisplayView alloc] initWithGap:labelGap
                                                                        sideSpace:(CGFloat)sideSpace
                                                                         topSpace:topSpace
                                                                      bottomSpace:bottomSpace
                                                                    paddingHeight:paddingHeight
                                                                         textFont:(UIFont *)textFont
                                                                        textColor:(UIColor *)textColor];
    return errorView;
}

- (instancetype)initWithGap:(CGFloat)labelGap
                  sideSpace:(CGFloat)sideSpace
                   topSpace:(CGFloat)topSpace
                bottomSpace:(CGFloat)bottomSpace
              paddingHeight:(CGFloat)paddingHeight
                   textFont:(UIFont *)textFont
                  textColor:(UIColor *)textColor {
    if (self = [super init]) {
        _labelGap = labelGap;
        _sideSpace = sideSpace;
        _topSpace = topSpace;
        _bottomSpace = bottomSpace;
        _paddingHeight = paddingHeight;
        _textFont = textFont;
        _textColor = textColor;
        [self updateWithErrors:nil];
    }
    return self;
}

- (void)updateWithErrors:(NSArray *)errors {
    if (self.errors&&self.errors==errors) {
        return ;
    }
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (!errors || errors.count<=0) {
        [self addPaddingView];
    } else {
        [self createLabelsForErrors:errors];
    }
    self.errors = errors;
}

- (void)addPaddingView {
    UIView *paddingView = [[UIView alloc] init];
    [self addSubview:paddingView];
    [paddingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.height.mas_equalTo(_paddingHeight);
    }];
}

- (void)createLabelsForErrors:(NSArray *)errors {
    UILabel *previousLabel;
    for (int i=0; i<errors.count; i++) {
        UILabel *label = [self defaultErrorLabelWithString:[errors objectAtIndex:i]];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.left.equalTo(self).offset(_sideSpace);
            make.right.equalTo(self).offset(-_sideSpace);
            if (i == 0) {
                make.top.equalTo(self).offset(_topSpace);
            } else {
                make.top.equalTo(previousLabel.mas_bottom).offset(_labelGap);
            }
            if (i == errors.count-1) {
                make.bottom.equalTo(self.mas_bottom).offset(-_bottomSpace);
            }
            make.height.mas_equalTo([self labelHeightForString:[errors objectAtIndex:i]]);
        }];
        previousLabel = label;
    }
}

- (CGFloat)labelHeightForString:(NSString *)value {
    CGRect infoRect = [value boundingRectWithSize:CGSizeMake(self.bounds.size.width - _sideSpace * 2, 1000)
                                          options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                       attributes:@{NSFontAttributeName : _textFont?:UIFontMake(13)}
                                          context:nil];
    NSUInteger lineNumber = infoRect.size.height / ((_textFont?:UIFontMake(13)).lineHeight);
    return lineNumber * (_textFont ? _textFont.lineHeight : UIFontMake(13).lineHeight) + 1;
}

- (UILabel *)defaultErrorLabelWithString:(NSString *)string {
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = _textFont?:UIFontMake(13);
    label.textColor = _textColor?:[UIColor tx_colorWithHex:0xff0000];
    label.text = string;
    [self addSubview:label];
    return label;
}

@end
